package logic

import (
	"cart/cart"
	"cart/internal/svc"
	"cart/model/mysql"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type AddCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCartLogic {
	return &AddCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加购物车

func (l *AddCartLogic) AddCart(in *cart.AddCartReq) (*cart.AddCartResp, error) {
	c := mysql.Cart{
		UserId:     in.UserId,
		GoodsId:    in.GoodsId,
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: in.GoodsPrice,
	}
	err := c.Add()
	if err != nil {
		logx.Error("购物车创建失败", err)
		return nil, err
	}
	return &cart.AddCartResp{CartId: int64(c.ID)}, nil
}
